package com.itfreer.print.base.itextpdf.textbefore;

import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itfreer.print.base.itextpdf.ChineseFontsProvider;
import com.itfreer.print.base.itextpdf.ITextBefore;

/**
 * 文字水印功能
 */
public class TextWaterMarkTextBefore implements ITextBefore {

	private String textWaterString;
	
	public TextWaterMarkTextBefore(String textWaterString){
		this.textWaterString = textWaterString;
	}
	
	/**
	 * 文档操作
	 * 
	 * @param pdfDocument
	 * @param pdfWriter
	 */
	public void doOptions(Document pdfDocument, PdfWriter pdfWriter) {
		if (pdfWriter == null) {
			return;
		}
		// 通过PDF页面事件模式添加文字水印功能
		pdfWriter.setPageEvent(new TextWaterMarkPdfPageEvent(textWaterString));
	}

	/**
	 * 为PDF分页时创建添加文本水印的事件信息
	 */
	private class TextWaterMarkPdfPageEvent extends PdfPageEventHelper {

		private String waterMarkText;

		public TextWaterMarkPdfPageEvent(String waterMarkText) {
			this.waterMarkText = waterMarkText;
		}

		public void onEndPage(PdfWriter writer, Document document) {
			try {
				float pageWidth = document.right() + document.left();// 获取pdf内容正文页面宽度
				float pageHeight = document.top() + document.bottom();// 获取pdf内容正文页面高度
				
				// 设置水印字体格式
				BaseFont bfChinese = ChineseFontsProvider.getYHBaseFont();
				Font waterMarkFont = new Font(bfChinese, 20);
				
				PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
				// 设置透明度为0.2
				PdfGState gs = new PdfGState();
				gs.setFillOpacity(0.2f);
				waterMarkPdfContent.setGState(gs);
				
				Phrase phrase = new Phrase(waterMarkText, waterMarkFont);
				ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase, 
						pageWidth * 0.25f, pageHeight * 0.2f, 45);
				ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase, 
						pageWidth * 0.25f, pageHeight * 0.5f, 45);
				ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase, 
						pageWidth * 0.25f, pageHeight * 0.8f, 45);
				ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase, 
						pageWidth * 0.65f, pageHeight * 0.2f, 45);
				ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase, 
						pageWidth * 0.65f, pageHeight * 0.5f, 45);
				ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase, 
						pageWidth * 0.65f, pageHeight * 0.8f, 45);
			} catch (DocumentException de) {
				de.printStackTrace();
				System.err.println("pdf watermark font: " + de.getMessage());
			} catch (IOException de) {
				de.printStackTrace();
				System.err.println("pdf watermark font: " + de.getMessage());
			}
		}
	}
}
